﻿Import-Module -Force ..\ConfigurationDatabase\libConfigurationDatabase.psm1

$scriptEnvironmentShortName="QA6"

$crashDumpArchivePath=(Get-ConfigurationSetting -environmentShortName $environmentShortName -scope "Global"  -name "CrashDumpArchivePath").Value

$crashDumpAnalysisLogPaths=Get-ChildItem $crashDumpArchivePath\*.log

$crashDumpAnalysisReports=@()

foreach($crashDumpAnalysisLogPath in $crashDumpAnalysisLogPaths)
{
    $crashDumpAnalysisLog=Get-Content $crashDumpAnalysisLogPath
    try
    {
        $operatingSystemName=($crashDumpAnalysisLog | Select-String "Kernel Version" | Out-String).Trim();
        if($operatingSystemName -ilike "* MP *")
        {
            $operatingSystemName=$operatingSystemName.Replace($operatingSystemName.Substring($operatingSystemName.IndexOf(" MP",0),$operatingSystemName.IndexOf(")",$operatingSystemName.IndexOf(" MP",0))-$operatingSystemName.IndexOf(" MP",0)+1),"")
        }
        else
        {
            $operatingSystemName=$operatingSystemName.Replace(" UP","")    
        }

        $operatingSystemName=$operatingSystemName.Replace(" Free","")
        $operatingSystemName=$operatingSystemName.Replace(" compatible","")

        $crashDumpAnalysisReport=New-Object PSObject -Property @{ComputerName=$crashDumpAnalysisLogPath.Name.Split("_")[0];
                                                                OperatingSystem=$operatingSystemName;
                                                                CrashDumpDate=$crashDumpAnalysisLogPath.FullName.Split("_")[1].Replace(".log","");
                                                                CrashDumpPath=$crashDumpAnalysisLogPath.FullName.Replace(".log",".DMP");
                                                                ProcessName=($crashDumpAnalysisLog | Select-String "PROCESS_NAME" | Out-String).Replace("PROCESS_NAME:  ","").Trim();
                                                                ImageName=($crashDumpAnalysisLog | Select-String "IMAGE_NAME" | Out-String).Replace("IMAGE_NAME:  ","").Trim();
                                                                ImageVersion=($crashDumpAnalysisLog | Select-String "IMAGE_VERSION" | Out-String).Replace("IMAGE_VERSION:  ","").Trim();
                                                                ModuleName=($crashDumpAnalysisLog | Select-String "MODULE_NAME" | Out-String).Replace("MODULE_NAME: ","").Trim();
                                                                FailureBucketId=($crashDumpAnalysisLog | Select-String "FAILURE_BUCKET_ID" | Out-String).Replace("FAILURE_BUCKET_ID:  ","").Trim()
                                                                }
    }
    catch
    {
        Write-Host "Error occured while analysing crash dump analysis log."
    }
    $crashDumpAnalysisReports+=$crashDumpAnalysisReport
}

$crashDumpAnalysisReports | Sort-Object -Property CrashDumpDate | select ComputerName,OperatingSystem,CrashDumpDate,ProcessName,ImageName,ImagerVersion,ModuleName,FailureBucketId,CrashDumpPath | Export-Csv -Path .\CrashDumpAnalysisReport.csv -Force -NoTypeInformation